﻿using BLL;
using BLL.Comm;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Inventory.sontemplate
{
    public partial class _Customer : Inventory.template.BaseInfoForm
    {
        private Customer c;
        private Contact contact;
        private CustomerBLL bl;
        private List<Customer> list;
       
        public _Customer()
        {
            InitializeComponent();
            bl = new CustomerBLL();
            dgvKhxx.AutoGenerateColumns = false;//使清空DataGridView控件数据源时，标题内容不会被清空
            BindData();//绑定DataGridView的数据源
            ControlOpera(false);
        }
        private void BindData()
        {
            dgvKhxx.DataSource = null;//清空DataGridView的数据源
            list = bl.GetList();
            dgvKhxx.DataSource = list;//将list集合数据绑定到控件dgvCustomer作为数据源
        }
        /// <summary>
        /// 定义单击 “新增/修改/删除”按钮式，GroupBox控件中TextBox的可操作性
        /// </summary>
        /// <param name="value"></param>
        private void ControlOpera(bool value)
        {
            tagKtxtKhbh.Enabled = value;
            tagKtxtKhnc.Enabled = value;
            tagKtxtDz.Enabled = value;
            tagKtxtYb.Enabled = value;
            tagKtxtDh.Enabled = value;
            tagKtxtCz.Enabled = value;
            tagKtxtKhyh.Enabled = value;
            tagKtxtYhzh.Enabled = value;
            tagKtxtLxr.Enabled = value;
            tagKtxtDzyj.Enabled = value;
        }
        /// <summary>
        /// 新增 按钮单击事件
        /// </summary>
        private void tagbtnXz_Click(object sender, EventArgs e)
        {
            tagKtxtPy.Enabled = false ;
            ControlOpera(true);
        }
        /// <summary>
        /// 将业务实体内容显示到TextBox控件中
        /// </summary>
        private void ShowInfo()
        {
            tagKtxtKhbh.Text = dgvKhxx.CurrentRow.Cells["Bh"].Value.ToString();
            tagKtxtKhnc.Text = dgvKhxx.CurrentRow.Cells["Mc"].Value.ToString();
            tagKtxtPy.Text = dgvKhxx.CurrentRow.Cells["SpellingCode"].Value.ToString();
            tagKtxtDz.Text = dgvKhxx.CurrentRow.Cells["Address"].Value.ToString();
            tagKtxtYb.Text = dgvKhxx.CurrentRow.Cells["ZipCode"].Value.ToString();
            tagKtxtDh.Text = dgvKhxx.CurrentRow.Cells["Tel"].Value.ToString();
            tagKtxtCz.Text = dgvKhxx.CurrentRow.Cells["Fax"].Value.ToString();
            tagKtxtKhyh.Text = dgvKhxx.CurrentRow.Cells["BankName"].Value.ToString();
            tagKtxtYhzh.Text = dgvKhxx.CurrentRow.Cells["BankAccount"].Value.ToString();
            tagKtxtLxr.Text = dgvKhxx.CurrentRow.Cells["Contacter"].Value.ToString();
            tagKtxtDzyj.Text = dgvKhxx.CurrentRow.Cells["Email"].Value.ToString();
            //tagKtxtKhbh.Text = c.CustomerID.Trim();
            //tagKtxtKhnc.Text = c.CustomerName;
            //tagKtxtPy.Text = c.SpellingCode.Trim();
            //tagKtxtDz.Text = c.Address.Trim();
            //tagKtxtYb.Text = c.ZipCode.Trim();
            //tagKtxtDh.Text = c.Tel.Trim();
            //tagKtxtCz.Text = c.Fax.Trim();
            //tagKtxtKhyh.Text = c.BankName.Trim();
            //tagKtxtYhzh.Text = c.BankAccount.Trim();
            //tagKtxtLxr.Text = c.Contacter.Trim();
            //tagKtxtDzyj.Text = c.Email.Trim();

        }
        /// <summary>
        /// 修改 按钮单击事件
        /// </summary>
        private void tagbtnXg_Click(object sender, EventArgs e)
        {
            ControlOpera(true  );
            tagKtxtPy.Enabled = false;
            tagKtxtKhbh.Enabled = false;
             ShowInfo();
              
        }
        /// <summary>
        /// 删除 按钮单击事件
        /// </summary>
        private void tagbtnSc_Click(object sender, EventArgs e)
        {
            tagKtxtPy.Enabled = false;
            ControlOpera(false );
            ShowInfo();

        }
        /// <summary>
        /// 文本框输入内容数据验证
        /// </summary>
        private string Validation()
        {
            if (tagKtxtKhbh.Text.Trim() == "") { tagKtxtKhbh.Focus(); return "客户编号不能为空值！"; }
            if (tagKtxtKhnc.Text.Trim() == "") { tagKtxtKhnc.Focus(); return "客户名称不能为空值！"; }
            if (tagKtxtDz.Text.Trim() == "") { tagKtxtDz.Focus(); return "地址不能为空值！"; }
            if (tagKtxtYb.Text.Trim() == "") { tagKtxtYb.Focus(); return "邮编不能为空值！"; }
            if (tagKtxtDh.Text.Trim() == "") { tagKtxtDh.Focus(); return "联系方式不能为空值！"; }
            if (tagKtxtCz.Text.Trim() == "") { tagKtxtCz.Focus(); return "传真不能为空值！"; }
            if (tagKtxtKhyh.Text.Trim() == "") { tagKtxtKhyh.Focus(); return "银行名称不能为空值！"; }
            if (tagKtxtYhzh.Text.Trim() == "") { tagKtxtYhzh.Focus(); return "银行账号不能为空值！"; }
            if (tagKtxtLxr.Text.Trim() == "") { tagKtxtLxr.Focus(); return "联系人不能为空值！"; }
            if (tagKtxtDzyj.Text.Trim() == "") { tagKtxtDzyj.Focus(); return "Email不能为空值！"; }
            return "";
        }
        /// <summary>
        /// 获取TextBox控件内容，并完成Customer实例化
        /// </summary>
        private Customer SetValue()
        {
            c = new Customer();
            c.CustomerID = tagKtxtKhbh.Text.Trim();
            c.CustomerName = tagKtxtKhnc.Text.Trim();
          
            c.SpellingCode = tagKtxtPy.Text.Trim();
            c.Address = tagKtxtDz.Text.Trim();
            c.ZipCode = tagKtxtYb.Text.Trim();
            c.Tel = tagKtxtDh.Text.Trim();
            c.Fax = tagKtxtCz.Text.Trim();
            c.BankName = tagKtxtKhyh.Text.Trim();
            c.BankAccount = tagKtxtYhzh.Text.Trim();
            c.Contacter = tagKtxtLxr.Text.Trim();
            c.Email = tagKtxtDzyj.Text.Trim();
           
            return c;
        }
        /// <summary>
        /// 存盘 按钮单击事件
        /// </summary>
        private void tagbtnCp_Click(object sender, EventArgs e)
        {
            string errorString = Validation();
            if (errorString.Length > 0) { MessageBox.Show(errorString, "系统提示"); return; }
            if (opera == Operation.INSERT)
            {
                SetValue();//获取控件值并完成对象赋值
                if (bl.Insert(c))
                {
                    BindData();//绑定DataGridView的数据源
                  
                    Clear();//清空TextBox控件内容
                    MessageBox.Show("新增数据成功！", "系统提示");
                }
                else
                    MessageBox.Show("新增数据失败！", "系统提示");
            }
            if (opera == Operation.UPDATE)
            {
                if (c != null)
                {
                    SetValue();//获取控件值并完成对象赋值
                    if (bl.Update(c))
                    {
                        BindData();//绑定DataGridView的数据源
                        ShowInfo();
                        Clear();//清空TextBox控件内容
                        MessageBox.Show("成功更新数据！", "系统提示");
                    }
                    else
                        MessageBox.Show("新增数据失败！", "系统提示");
                }
                else
                    MessageBox.Show("请选择需要修改的记录", "系统提示");
            }
            if (opera == Operation.DELETE)
            {
                if (c != null)
                {
                    SetValue();//获取控件值并完成对象赋值
                    if (bl.Delete(c ))
                    {
                        BindData();//绑定DataGridView的数据源
                      
                        Clear();//清空TextBox控件内容
                        MessageBox.Show("成功删除数据！", "系统提示");
                    }
                    else
                        MessageBox.Show("删除数据失败！", "系统提示");
                }
                else
                    MessageBox.Show("请选择需要删除的记录", "系统提示");
            }
        }
        private void dgvKhxx_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvKhxx.CurrentRow.Selected = true;
            c = list[e.RowIndex];
            if (opera == Operation.UPDATE || opera == Operation.DELETE)
                ShowInfo();
        }
        
        private void tagKtxtYb_TextChanged(object sender, EventArgs e)
        {

        }

        private void tagKtxtKhbh_TextChanged(object sender, EventArgs e)
        {
           
        }

        private void dgvKhxx_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {

            //if (dgvKhxx.Columns[e.ColumnIndex].Name == "SpellingCode")
            //    e.Value = ((Customer)e.Value).SpellingCode;
            //if (dgvKhxx.Columns[e.ColumnIndex].Name == "Address")
            //    e.Value = ((Customer)e.Value).Address;
            //if (dgvKhxx.Columns[e.ColumnIndex].Name == "ZipCode")
            //    e.Value = ((Customer)e.Value).ZipCode;
            //if (dgvKhxx.Columns[e.ColumnIndex].Name == "Tel")
            //    e.Value = ((Customer)e.Value).Tel;
            //if (dgvKhxx.Columns[e.ColumnIndex].Name == "Fax")
            //    e.Value = ((Customer)e.Value).Fax;
            //if (dgvKhxx.Columns[e.ColumnIndex].Name == "BankName")
            //    e.Value = ((Customer)e.Value).BankName;
            //if (dgvKhxx.Columns[e.ColumnIndex].Name == "BankAccount")
            //    e.Value = ((Customer)e.Value).BankAccount;
            //if (dgvKhxx.Columns[e.ColumnIndex].Name == "Contacter")
            //    e.Value = ((Customer)e.Value).Contacter;
            //if (dgvKhxx.Columns[e.ColumnIndex].Name == "Email")
            //    e.Value = ((Customer)e.Value).Email;
        }

        private void tagKtxtKhnc_TextChanged(object sender, EventArgs e)
        {
            tagKtxtPy.Text = PY.HZToSpell(tagKtxtKhnc.Text.Trim());
        }
    }
}
